package com.atguigu.common.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName ListValueConstraintValidator
 * @Description
 * @Author LiQianJin~
 * @Date 2024/3/25 0:21
 * @Version 1.0
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {

    private Set<Integer> setValues = new HashSet<>();

    // 初始化方法
    @Override
    public void initialize(ListValue constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
        int[] values = constraintAnnotation.values();
        if (values != null) {
            for (int value : values) {
                setValues.add(value);
            }
        }
    }


    /***
     * @Description: 判断校验注解是否成功
     * @Author: LiQianJin
     * @Date: 2024/3/25 0:24
     * @param value: 需要校验的值
     * @param context: 上下文
     * @return: boolean
     **/
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return setValues.contains(value);
    }
}
